package com.idopartx.phonelightning.ui.permissionguide

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.idopartx.phonelightning.R
import com.zhpan.indicator.IndicatorView
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle

open class HuaweiAutoStartGuideDialog(context: Context, callback: DialogCallBack?) :
    Dialog(context) {
    var callback: DialogCallBack? = null
    var mCurIndex = 1

    init {
        this.callback = callback
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val window = window
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setContentView(R.layout.dialog_guide)

        val layoutParams = window!!.attributes
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
        window.attributes = layoutParams

        setCanceledOnTouchOutside(false)

        initBannerView()

        findViewById<Button>(R.id.btn_start).setOnClickListener {
            callback?.onOk(mCurIndex)
            dismiss()
        }

        findViewById<ImageView>(R.id.img_close).setOnClickListener { dismiss() }
    }

    private fun initBannerView() {
        val mBanner = findViewById<ViewPager2>(R.id.banner)
        val list = ArrayList<Int>()
        list.add(R.mipmap.guide_huawei_1)
        list.add(R.mipmap.guide_huawei_2)
        list.add(R.mipmap.guide_huawei_3)
        val mAdapter = ImageAdapter()
        mBanner?.adapter = mAdapter
        mAdapter.setList(list)

        val qyIndicator = findViewById<IndicatorView>(R.id.indicator)
        qyIndicator
            .setSliderWidth(context.resources.getDimension(R.dimen.dp_8))
            .setSliderHeight(context.resources.getDimension(R.dimen.dp_8))
            .setSliderGap(context.resources.getDimension(R.dimen.dp_12))
            .setSlideMode(IndicatorSlideMode.SMOOTH)
            .setIndicatorStyle(IndicatorStyle.CIRCLE)
            .setupWithViewPager(mBanner)

        mBanner?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> {
                        findViewById<TextView>(R.id.txt_content).text = "1.点击应用启动管理";
                    }
                    1 -> {
                        findViewById<TextView>(R.id.txt_content).text = "2.找到来电闪光灯并关闭自动管理";
                    }
                    2 -> {
                        findViewById<TextView>(R.id.txt_content).text = "3.开启图中三个按钮";
                    }
                }
            }
        })
    }

    inner class ImageAdapter :
        BaseQuickAdapter<Any, BaseViewHolder>(R.layout.item_image) {
        override fun convert(holder: BaseViewHolder, item: Any) {
            val iv = holder.getView<ImageView>(R.id.iv_content)
            iv.scaleType = ImageView.ScaleType.FIT_CENTER
            Glide.with(iv).load(item).into(iv)
            //内部实现点击事件
            iv.setOnClickListener {
//                when (index) {
//
//                }
            }
        }

    }


    interface DialogCallBack {
        fun onOk(index: Int)
    }

}